package com.test.daily.leetcode.y2022.m07.day0705.v02;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/7/5 上午 11:22
 * @version: 1.0
 */
public class Solution {
    public static class StackImplementByRingArray{
        public int polli;
        public int pushi;
        public int limit;
        public int size;
        public int[] arr;
        public StackImplementByRingArray(int limit){
            this.size = 0;
            this.limit = limit;
            this.arr = new int[limit];
            this.polli = 0;
            this.pushi = 0;
        }
        public void push(int value){
            if(this.size == this.limit){
                throw new RuntimeException("queue is full");
            }
            this.size++;
            this.arr[pushi] = value;
            this.pushi = nextIndex(this.pushi);
        }
        public int pop(){
            if(this.size == 0){
                throw new RuntimeException("queue is empty.");
            }
            int ans = this.arr[polli];
            this.size--;
            this.polli = nextIndex(this.polli);
            return ans;
        }

        public int nextIndex(int index){
            if(index < this.limit -1){
                return index + 1;
            } else {
                return 0;
            }
        }
    }
}
